package com.wenx.v3system.modular.cloud.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenx.v3system.modular.cloud.domain.po.SysRoleMenu;
import com.wenx.v3system.modular.cloud.mapper.SysRoleMenuMapper;
import com.wenx.v3system.modular.cloud.service.SysRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysRoleMenuServiceImpl
        extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Override
    public List<Long> getMenuIdsByRoleId(Long roleId) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> roleMenus = list(wrapper);
        return roleMenus.stream()
                .map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
    }

    @Override
    public List<Long> getRoleIdsByMenuId(Long menuId) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getMenuId, menuId);
        List<SysRoleMenu> roleMenus = list(wrapper);
        return roleMenus.stream()
                .map(SysRoleMenu::getRoleId)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void assignMenusToRole(Long roleId, List<Long> menuIds) {
        // 先删除角色的现有菜单
        removeRoleMenus(roleId);
        
        // 添加新的菜单关联
        if (CollUtil.isNotEmpty(menuIds)) {
            List<SysRoleMenu> roleMenus = menuIds.stream()
                    .map(menuId -> {
                        SysRoleMenu roleMenu = new SysRoleMenu();
                        roleMenu.setRoleId(roleId);
                        roleMenu.setMenuId(menuId);
                        return roleMenu;
                    })
                    .collect(Collectors.toList());
            saveBatch(roleMenus);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeRoleMenus(Long roleId) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId, roleId);
        // 使用物理删除避免唯一约束冲突
        int deletedCount = baseMapper.delete(wrapper);
        log.info("物理删除角色菜单关联记录数: {}, 角色ID: {}", deletedCount, roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeMenuRoles(Long menuId) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getMenuId, menuId);
        // 使用物理删除避免唯一约束冲突
        int deletedCount = baseMapper.delete(wrapper);
        log.info("物理删除菜单角色关联记录数: {}, 菜单ID: {}", deletedCount, menuId);
    }
}
